package com.vitabuybuy.tv.network

import com.library.common.net.NetUrl
import com.vitabuybuy.tv.dto.CartGoodsDetailsDto
import com.vitabuybuy.tv.dto.CouponCenterDto
import com.vitabuybuy.tv.dto.SettlementDto
import com.vitabuybuy.tv.dto.SubmitIdDto
import com.vitabuybuy.tv.ui.cart.ConfirmOrderActivity
import rxhttp.IAwait
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.Map
import kotlin.collections.MutableList
import kotlin.collections.mapOf
import kotlin.collections.set

/**
 * 作者：SimGa
 * 日期：2020/12/8
 * 时间：11:42
 * 包名：com.vitabuybuy.tv.network
 */
object CartRepository {


    fun getCartGoodsDetails(): IAwait<MutableList<CartGoodsDetailsDto>> {
        return RxHttp.postJson(NetUrl.api_get_cart_all_msg).toResponse()
    }

    fun addCartQuantity(cartId: Int): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_add_cart_quantity)
            .add("id", cartId)
            .add("quantity", 1)
            .toResponse()
    }

    fun reduceCartQuantity(cartId: Int): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_reduce_cart_quantity)
            .add("id", cartId)
            .add("quantity", 1)
            .toResponse()
    }

    fun deleteCart(idList: ArrayList<Int>): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_delete_cart)
            .add("idList", idList)
            .toResponse()
    }

    fun calculateCart(idList: ArrayList<Int>?, from: Int?): IAwait<SettlementDto> {
        val list = ArrayList<HashMap<String, Any>>()
        var id: Int? = 0
        idList?.run {
            if (from == ConfirmOrderActivity.SETTLEMENT_ORDER) {
                for (ids in this) {
                    val mapOf = HashMap<String, Any>()
                    mapOf["id"] = ids
                    list.add(mapOf)
                }
            } else {
                id = this[0]
            }
        }
        return if (from == ConfirmOrderActivity.SETTLEMENT_ORDER) {
            RxHttp.postJson(NetUrl.api_calculate)
                .add("itemList", list)
                .toResponse()
        } else {
            RxHttp.postJson(NetUrl.api_buy_again)
                .add("id", id)
                .toResponse()
        }
    }

    fun foundCanUserCoupons(params: Map<String, Any?>): IAwait<MutableList<CouponCenterDto>> {
        return RxHttp.postJson(NetUrl.api_submint_order_can_user_coupon)
            .addAll(params).toResponse()
    }

    fun submitOrder(
        idList: ArrayList<Int>?,
        couponIdList: ArrayList<Int>?,
        id: Int?,
        email: String,
        remark: HashMap<Int, String>,
    ): IAwait<SubmitIdDto> {
        val list = ArrayList<HashMap<String, Any>>()
        idList?.run {
            for (ids in this) {
                val mapOf = HashMap<String, Any>()
                mapOf["id"] = ids
                list.add(mapOf)
            }
        }
        return RxHttp.postJson(NetUrl.api_create_order)
            .add("itemList", list)
            .add("couponIdList", couponIdList)
            .add("address", mapOf(Pair("id", id)))
            .add("email", email)
            .add("remarkMap", remark)
            .toResponse()
    }
    fun submitOrder(
        idList: ArrayList<Int>?,
        couponIdList: ArrayList<Int>?,
        id: Int?,
        email: String,
        remark: HashMap<Int, String>,
        liveId:Int?
    ): IAwait<SubmitIdDto> {
        val list = ArrayList<HashMap<String, Any>>()
        idList?.run {
            for (ids in this) {
                val mapOf = HashMap<String, Any>()
                mapOf["id"] = ids
                list.add(mapOf)
            }
        }
        return RxHttp.postJson(NetUrl.api_create_order)
            .add("itemList", list)
            .add("couponIdList", couponIdList)
            .add("address", mapOf(Pair("id", id)))
            .add("email", email)
            .add("liveId", liveId)
            .add("remarkMap", remark)
            .toResponse()
    }

    fun orderPay(id: Int?, paymentMethod: String): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_order_pay)
            .add("id", id)
            .add("paymentMethod", paymentMethod)
            .toResponse()
    }

    fun buyNow(goodId: Int?, quantity: Int?): IAwait<SettlementDto> {
        return RxHttp.postJson(NetUrl.api_buy)
            .add("goods", mapOf(Pair("id", goodId)))
            .add("number", quantity)
            .toResponse()
    }

    fun buyNow(goodId: Int?, liveId: Int?, quantity: Int?): IAwait<SettlementDto> {
        return RxHttp.postJson(NetUrl.api_buy)
            .add("goods", mapOf(Pair("id", goodId)))
            .add("number", quantity)
            .add("liveId", liveId)
            .toResponse()
    }

    fun buyByGroup(goodId: Int?, quantity: Int?): IAwait<SettlementDto> {
        return RxHttp.postJson(NetUrl.api_group_buy)
            .add("goods", mapOf(Pair("id", goodId)))
            .add("number", quantity)
            .toResponse()
    }

    fun submitGroupBuyCreate(
        itemId: Int,
        groupId: Int?,
        addressId: Int?,
        email: String,
        remark: HashMap<Int, String>
    ): IAwait<SubmitIdDto> {
        return RxHttp.postJson(NetUrl.api_group_buy_creat)
            .add("groupBuying", mapOf(Pair("id", groupId)))
            .add("item", mapOf(Pair("id", itemId)))
            .add("address", mapOf(Pair("id", addressId)))
            .add("email", email)
            .add("remarkMap", remark)
            .toResponse()
    }

    fun submitGroupBuyJoin(
        itemId: Int,
        groupId: Int?,
        addressId: Int?,
        email: String,
        remark: HashMap<Int, String>
    ): IAwait<SubmitIdDto> {
        return RxHttp.postJson(NetUrl.api_group_buy_join)
            .add("purchasingGroup", mapOf(Pair("id", groupId)))
            .add("item", mapOf(Pair("id", itemId)))
            .add("address", mapOf(Pair("id", addressId)))
            .add("email", email)
            .add("remarkMap", remark)
            .toResponse()
    }

    fun submitGroupBuyBylimitJoin(
        itemId: Int,
        groupId: Int?,
        addressId: Int?,
        email: String,
        remark: HashMap<Int, String>
    ): IAwait<SubmitIdDto> {
        return RxHttp.postJson(NetUrl.api_group_buy_limit_join)
            .add("groupBuying", mapOf(Pair("id", groupId)))
            .add("item", mapOf(Pair("id", itemId)))
            .add("address", mapOf(Pair("id", addressId)))
            .add("email", email)
            .add("remarkMap", remark)
            .toResponse()
    }


    fun getCouponsList(
        storeId: Int?
    ): IAwait<MutableList<CouponCenterDto>> {
        val map = HashMap<String, Any>()
        map["store"] = mapOf(
            Pair("id", storeId)
        )
        return RxHttp.postJson(NetUrl.api_find_coupon_center_list)
            .addAll(map)
            .toResponse()
    }

    fun saveACoupon(
        couponId: Int
    ): IAwait<String> {
        return RxHttp.postJson(NetUrl.api_save_a_coupon)
            .add(
                "userCouponRecord", mapOf(
                    Pair(
                        "coupon", mapOf(
                            Pair("id", couponId)
                        )
                    )
                )
            ).toResponse()
    }

    fun calculateAmount(params: HashMap<String, Any?>): IAwait<SettlementDto> {
        return RxHttp.postJson(NetUrl.api_calculate_amount)
            .addAll(params)
            .toResponse()
    }
}
